﻿using System;
using UtilLib;

namespace Server.Core
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {             
                var serverHost = new ServerHost();
                serverHost.Open();

                Console.WriteLine("PRESS [ENTER] TO QUIT.");
                Console.ReadLine();

                serverHost.Close();

                CountDown(() =>
                {
                    Environment.Exit(0);
                });

                Console.ReadLine();
            }
            catch (Exception e)
            {
                EventLogger.LogError(e);
            }
        }

        private static void CountDown(Action action)
        {
            var count = 6;
            var timer = new System.Timers.Timer(1000);
            timer.Elapsed += (s, e) =>
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write($"请稍后({--count})...");
                if (count <= 0)
                {
                    action();
                    timer.Stop();
                }
            };

            timer.Start();
        }
    }
}
